﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using System.Collections;
using System.Collections.Generic;

namespace Zwms.Client.InfoSys
{
    public partial class SysRoleForm : BaseForm
    {
        #region 成员变量
        public SysRole CurrentSysRole { get; set; }
        private EnumOpenType OpenType { get; set; }
        #endregion

        #region 构造函数
        public SysRoleForm(EnumOpenType opentype, string sRoleCode)
        {
            InitializeComponent();
            OpenType = opentype;
            
            if (opentype == EnumOpenType.Open)
            {
                tbRoleCode.ReadOnly = true;
                //查询角色信息
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                SysRole record = new SysRole();
                record.Id = sRoleCode;
                CurrentSysRole = client.QuerySysRole(record);
            }
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
        }
        private void InitControl()
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            IList<SysMenu> menuList = client.GetMenuByRoleCode(CommonDefine.sSysAdmingRoleCode);
            clbAuthorityList.DataSource = menuList;
            clbAuthorityList.ValueMember = "MenuName";

            if (this.OpenType == EnumOpenType.New)
            {
                CurrentSysRole = new SysRole();
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadInfo(CurrentSysRole);

            }
        }

        private void LoadInfo(SysRole record)
        {
            this.tbRoleCode.Text = record.Id;
            this.tbRoleName.Text = record.RoleName;
            this.tbRemark.Text = record.Remark;

            //获得权限列表
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            IList<SysMenu> menuList = client.GetMenuByRoleCode(record.Id);
            for(int i=0;i<clbAuthorityList.Items.Count;i++) 
            {
                SysMenu menu = clbAuthorityList.Items[i] as SysMenu;
                foreach (SysMenu querymenu in menuList)
                {
                    if (menu.MenuCode == querymenu.MenuCode)
                    {
                        clbAuthorityList.SetItemChecked(i, true);
                    }
                }
            }
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (this.tbRoleCode.Text == CommonDefine.sSysAdmingRoleCode)
            {
                MessageBox.Show("该编号为系统管理员预留编号，请使用其他编号！");
                return;
            }

            //数据完整性检查
            bool bRet = true;
            if (this.tbRoleCode.Text.Trim() == "")
            {
                bRet = false;
                this.tbRoleCode.BackColor = Color.Yellow;
            }
            if (this.tbRoleName.Text.Trim() == "")
            {
                bRet = false;
                this.tbRoleName.BackColor = Color.Yellow;
            }
            if (!bRet)
            {
                return;
            }

            if (MessageBox.Show("您确定要添加（修改）该角色吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                SysRole record = new SysRole();
                record.Id = this.tbRoleCode.Text.Trim();
                record.RoleName = this.tbRoleName.Text.Trim();
                record.Remark = this.tbRemark.Text.Trim();
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                if (this.OpenType == EnumOpenType.New)
                {
                    bRet = client.AddSysRole(record);
                }
                else
                {
                    bRet = client.UpdateSysRole(record);
                }

                if (bRet)
                {
                    if (client.DeleteMenuList(client.GetMenuByRoleCode(record.Id)))
                    {
                        List<SysMenu> menuList = new List<SysMenu>();
                        for (int i = 0; i < clbAuthorityList.CheckedItems.Count; i++)
                        {
                            SysMenu menu = clbAuthorityList.CheckedItems[i] as SysMenu;
                            menu.RoleCode = record.Id;
                            menuList.Add(menu);
                        }

                        if (client.AddMenuList(menuList.ToArray()))
                        {
                            MessageBox.Show("成功!");
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                        else
                        {
                            MessageBox.Show("添加菜单出错！");
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("删除原有菜单出错！");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("未能添加，请检查编号是否已存在！");
                    return;
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion
    }
}
